Assignment 1: Building a Better Contact Sheet

In the lectures for this week you were shown how to make a contact sheet for digital photographers, and how you can take one image and create nine different variants based on the brightness of that image. In this assignment you are going to change the colors of the image, creating variations based on a single photo. There are many complex ways to change a photograph using variations, such as changing a black and white image to either "cool" variants, which have light purple and blues in them, or "warm" variants, which have touches of yellow and may look sepia toned. In this assignment, you'll be just changing the image one color channel at a time

Your assignment is to learn how to take the stub code provided in the lecture (cleaned up below), and generate the following output image:

From the image you can see there are two parameters which are being varied for each sub-image. First, the rows are changed by color channel, where the top is the red channel, the middle is the green channel, and the bottom is the blue channel. Wait, why don't the colors look more red, green, and blue, in that order? Because the change you to be making is the ratio, or intensity, or that channel, in relationship to the other channels. We're going to use three different intensities, 0.1 (reduce the channel a lot), 0.5 (reduce the channel in half), and 0.9 (reduce the channel only a little bit).

For instance, a pixel represented as (200, 100, 50) is a sort of burnt orange color. So the top row of changes would create three alternative pixels, varying the first channel (red). one at (20, 100, 50), one at (100, 100, 50), and one at (180, 100, 50). The next row would vary the second channel (blue), and would create pixels of color values (200, 10, 50), (200, 50, 50) and (200, 90, 50).

Note: A font is included for your usage if you would like! It's located in the file readonly/fanwood-webfont.ttf

Need some hints? Use them sparingly, see how much you can get done on your own first! The sample code given in the class has been cleaned up below, you might want to start from that.

In [26]:
import PIL
from PIL import Image
from PIL import ImageEnhance
from PIL import ImageDraw
from PIL import ImageFont

def mod_RGB(input_image, channel, setting):
    """Brief:   This function takes an image object as input
                and returns a modifed version by modifying the
                saturation level of different R,G,B channels
       Param:   channel, which channel to be modified: (R,G,B)
       Param:   setting, the ammount (from 0 to 1) by which to
                modify the respective channel
       RetVal:  output_image, a modified version of input image"""
    output_image = input_image.copy()
    for x in range(input_image.width):
        for y in range(input_image.height):
            (R,G,B) = input_image.getpixel((x,y))
            if channel == "R":
                R = int(R * setting)
            elif channel == "G":
                G = int(G * setting)
            else:
                B = int(B * setting)
            output_image.putpixel((x,y), (R,G,B))
    return output_image

def get_fill_color(channel, setting):
    """Brief:   Function used to get the fill color for the text
                to be written under the image. Start with white
                and depending on input parameters modifies the
                respective channel"""
    (R,G,B) = (255,255,255) # start off with white
    if channel == "R":
        R = int(R * setting)
    elif channel == "G":
        G = int(G * setting)
    else:
        B = int(B * setting)
    return (R,G,B)

# color_list used to define the channels and settings to be applied            
COLOR_LIST = [('R', 0.1), ('R', 0.5), ('R', 0.9),
             ('G', 0.1), ('G', 0.5), ('G', 0.9),
             ('B', 0.1), ('B', 0.5), ('B', 0.9)]
# font size for text
FONT_SIZE = 45

# read image and convert to RGB
image=Image.open("readonly/msi_recruitment.gif")
image=image.convert('RGB')

# build a list of 9 images
images=[]
for i in range(9):
    # modify RGB saturation
    modified_image = mod_RGB(image, *COLOR_LIST[i])
    # create a new slightly larger image to fit the text at the bottom
    new_image = Image.new('RGB', (modified_image.width, modified_image.height + FONT_SIZE))
    temp_draw = ImageDraw.Draw(new_image)
    # copy - paste the modified image onto the new image
    new_image.paste(modified_image, (0,0))
    # write text on image
    fnt = ImageFont.truetype('readonly/fanwood-webfont.ttf', FONT_SIZE-2)
    formated_string ="Channel '{}', Intensity '{}'".format(COLOR_LIST[i][0], COLOR_LIST[i][1])
    temp_draw.text((10, new_image.height-FONT_SIZE+6), formated_string, font = fnt, fill=get_fill_color(*COLOR_LIST[i]))
    # display image (for debugging)
    display(new_image)
    images.append(new_image)
In [27]:
# create a contact sheet
first_image=images[0]
contact_sheet=PIL.Image.new(first_image.mode, (first_image.width*3,first_image.height*3))
x=0
y=0

for img in images:
    # Lets paste the current image into the contact sheet
    contact_sheet.paste(img, (x, y) )
    # Now we update our X position. If it is going to be the width of the image, then we set it to 0
    # and update Y as well to point to the next "line" of the contact sheet.
    if x+first_image.width == contact_sheet.width:
        x=0
        y=y+first_image.height
    else:
        x=x+first_image.width

# resize and display the contact sheet
contact_sheet = contact_sheet.resize((int(contact_sheet.width/2),int(contact_sheet.height/2) ))
display(contact_sheet)
contact_sheet.save("assignment_1.png")
In [ ]:
 

HINT 1

Check out the PIL.ImageDraw module for helpful functions

HINT 2

Did you find the text() function of PIL.ImageDraw?

HINT 3

Have you seen the PIL.ImageFont module? Try loading the font with a size of 75 or so.

HINT 4

These hints aren't really enough, we should probably generate some more.